﻿using facebook.businuss.netcore.sdk.Model;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace facebook.businuss.netcore.sdk.Sdk
{
    public class APIContext
    {
        public static readonly string DEFAULT_API_BASE = "https://graph.facebook.com";
        public static readonly string DEFAULT_API_VERSION = "v12.0";
        public static readonly string DEFAULT_VIDEO_API_BASE = "https://graph-video.facebook.com";
        private string endpointBase;
        private string videoEndpointBase;
        private string accessToken;
        private string appSecret;
        private string appID;
        private string version;
        protected bool isDebug;
        //bmId
        protected string bmId;
        protected readonly ILogger<APIContext> _logger;

        public  WebProxy Proxy;

        public APIContext(String endpointBase, String videoEndpointBase, String version, String accessToken, String appSecret, String appID, bool logCrash, WebProxy proxy)
        {
            this.isDebug = false;
            this.version = version;
            this.endpointBase = endpointBase;
            this.videoEndpointBase = videoEndpointBase;
            this.accessToken = accessToken;
            this.appSecret = appSecret;
            this.appID = appID;
            this.Proxy = proxy;
            //if (logCrash)
            //{
            //    CrashReporter.enable(this);
            //}

        }

        public APIContext(String accessToken, WebProxy proxy)
            : this("https://graph.facebook.com", "https://graph-video.facebook.com", DEFAULT_API_VERSION, accessToken, (String)null, null, true, proxy)
        {

        }

        //public APIContext(String accessToken, String appSecret,WebProxy proxy)
        //    : this("https://graph.facebook.com", "https://graph-video.facebook.com", "v11.0", accessToken, appSecret,null, true, proxy)
        //{

        //}

        public APIContext(String accessToken, String appSecret, String appID, WebProxy proxy)
            : this("https://graph.facebook.com", "https://graph-video.facebook.com", DEFAULT_API_VERSION, accessToken, appSecret, appID, true, proxy)
        {

        }

        public APIContext(String accessToken, String appSecret, String appID, bool logCrash, WebProxy proxy) :
            this("https://graph.facebook.com", "https://graph-video.facebook.com", DEFAULT_API_VERSION, accessToken, appSecret, appID, logCrash, proxy)
        {
        }

        public APIContext(String accessToken,string bmId, WebProxy proxy) 
        {
            this.version = DEFAULT_API_VERSION;
            this.endpointBase = DEFAULT_API_BASE;
            this.bmId = bmId;
            this.accessToken = accessToken;
            this.Proxy = proxy;
        }

        public String getEndpointBase()
        {
            return this.endpointBase;
        }

        public String getVideoEndpointBase()
        {
            return this.videoEndpointBase;
        }

        public String getAccessToken()
        {
            return this.accessToken;
        }

        public String getAppSecret()
        {
            return this.appSecret;
        }

        public bool hasAppSecret()
        {
            return this.appSecret != null;
        }

        //public String getAppSecretProof()
        //{
        //    return sha256(this.appSecret, this.accessToken);
        //}

        public String getVersion()
        {
            return this.version;
        }

        public bool IsDebug()
        {
            return this.isDebug;
        }

        public APIContext enableDebug(bool isDebug)
        {
            this.isDebug = isDebug;
            return this;
        }

        //public PrintStream getLogger()
        //{
        //    return this.logger;
        //}

        //public APIContext setLogger(PrintStream logger)
        //{
        //    this.logger = logger;
        //    return this;
        //}

        //public void log(String s)
        //{
        //    if (this.isDebug && this.logger != null)
        //    {
        //        this.logger.println(s);
        //    }

        //}

        //public static String sha256(String secret, String message)
        //{
        //    try
        //    {
        //        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        //        SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
        //        sha256HMAC.init(secretKey);
        //        byte[] bytes = sha256HMAC.doFinal(message.getBytes());
        //        return toHex(bytes);
        //    }
        //    catch (Exception var5)
        //    {
        //        throw new IllegalArgumentException("The provided app secret or access token is invalid!");
        //    }
        //}

        //public static String toHex(byte[] bytes)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    byte[] var2 = bytes;
        //    int var3 = bytes.length;

        //    for (int var4 = 0; var4 < var3; ++var4)
        //    {
        //        byte b = var2[var4];
        //        sb.append(String.format("%1$02x", b));
        //    }

        //    return sb.toString();
        //}

        public String getAppID()
        {
            if (this.appID != null)
            {
                return this.appID;
            }
            else
            {
                if (this.accessToken != null)
                {
                    try
                    {
                        DefaultRequestExecutor executor = new DefaultRequestExecutor();
                        string apiUrl = this.endpointBase + "/" + this.version + "/debug_token";
                        Dictionary<string, object> param = new Dictionary<string, object>();
                        param.Add("input_token", this.accessToken);
                        param.Add("access_token", this.accessToken);
                        param.Add("fields", "app_id");
                        ResponseWrapper response = executor.execute("GET", apiUrl, param, this);
                        //JsonParser parser = new JsonParser();
                        //this.appID = parser.parse(response.getBody()).getAsJsonObject().get("data").getAsJsonObject().get("app_id").getAsString();
                        return this.appID;
                    }
                    catch (Exception var6)
                    {
                        //this.log("Unable to fetch appID from the access token");
                    }
                }

                return null;
            }
        }

        //public static void disableCrashReport()
        //{
        //    CrashReporter.disable();
        //}
    }
}
